package com.poposchool.project.service.impl;

import com.poposchool.project.common.Constants;
import com.poposchool.project.mapper.RoleMapper;
import com.poposchool.project.mapper.RoleToMenuMapper;
import com.poposchool.project.exception.ServiceException;
import com.poposchool.project.entity.Role;
import com.poposchool.project.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Wang Jie
 * @Date 2022/7/4 16:31
 * @Version 1.0
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Resource
    RoleToMenuMapper roleToMenuMapper;


    @Override
    public List<Role> getRoles() {
        List<Role> roles;
        try {
            roles = roleMapper.findAll();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return roles;
    }

    @Override
    public Role getRoleById(Integer id) {
        Role role;
        try {
            role = roleMapper.getRoleById(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return role;
    }

    @Override
    public void deleteRole(Integer id) {
        try {
            roleMapper.deleteUser(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void updateRole(Role role) {
        try {
            roleMapper.updateRole(role);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void insertRole(Role role) {
        try {
            roleMapper.insertRole(role);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public Integer getAllNum() {
        Integer allNum;
        try {
            allNum = roleMapper.getAllNum();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return allNum;
    }

    @Override
    public List<Role> getPageRoles(Integer pageNum, Integer pageSize, String name) {
        List<Role> roles;
        try {
            roles = roleMapper.getPageRoles((pageNum - 1) * pageSize,pageSize,name);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return roles;
    }

    @Override
    public void batchDeleteRoles(List<Integer> ids) {
        try {
            roleMapper.batchDeleteRoles(ids);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void insertRoles(List<Role> roles) {
        try {
            roleMapper.insertRoles(roles);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void roleMenu(Integer roleId, List<Integer> menuIds) {
        try {
            log.info("roleId = "+roleId + "  menuIds = "+menuIds.toString());
            roleToMenuMapper.insertCon(roleId,menuIds);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public List<Integer> getRoleMenu(Integer roleId) {
        try {
            return roleToMenuMapper.getRoleMenu(roleId);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void deleteRoleMenuById(Integer roleId) {
        try {
            roleToMenuMapper.deleteRoleById(roleId);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }
}
